package com.qianxx.component.activity.recycleview.pull_recyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.recyclerview.pull.DIRECTION;
import com.qianxx.viewlibrary.recyclerview.pull.PullListener;
import com.qianxx.viewlibrary.recyclerview.pull.PullRecyclerView;
import com.qianxx.viewlibrary.recyclerview.pull.ScrollListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

public class PullRecyclerViewActivity extends BaseActivity
        implements PullListener, ScrollListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    PullRecyclerView rv;

    PullRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_recycler_view);
        ButterKnife.bind(this);

        adapter = new PullRecyclerViewAdapter(this);
        rv.setHeadRefreshView(new RefreshHeadView(this));//设置头部刷新控件
        rv.setMoreRefreshView(new LoadMoreView(this));//设置底部刷新控件
        rv.setUseLoadMore(true);//底部刷新控件是否可用
        rv.setUseRefresh(true);//头部刷新控件是否可用
        rv.setPullLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);//适配器
        rv.setPullListener(this);
        rv.setScollListener(this);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("");
        }
        adapter.setAll(list);
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (rv != null)
                    rv.onRefreshComplete();
            }
        }, 2000);
    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (rv != null)
                    rv.onLoadMoreComplete();
            }
        }, 2000);
    }

    /**
     * 监听刷新头下拉偏移量
     *
     * @param y 下拉偏移量
     */
    @Override
    public void onPull(int y) {
        tvTitle.setVisibility(y <= 0 ? VISIBLE : GONE);
    }

    /**
     * 列表滚动事件
     *
     * @param y                        偏移量
     * @param direction                滚动方向
     * @param firstVisibleItemPosition 当前首个可见元素的在列表中的位置
     * @param lastVisibleItemPosition  当前最后一个可见元素的在列表中的位置
     */
    @Override
    public void onScrolled(int y, DIRECTION direction, int firstVisibleItemPosition, int lastVisibleItemPosition) {

    }

    @Override
    public String setTitle() {
        return "PullRecyclerView";
    }
}
